package cn.qn.controller;

import cn.qn.common.util.UserSubjectUtil;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.user.*;
import cn.qn.enums.SmsTemplatesEnum;
import cn.qn.exceptions.BizException;
import cn.qn.moudle.user.AppUser;
import cn.qn.service.IAppUserService;
import cn.qn.vo.AppUserResetPassVo;
import cn.qn.vo.AppUserVo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Bryant
 * @date: 2019/7/15
 * @description:
 */
@RestController
@RequestMapping("/user")
public class AppUserController {

    @Autowired
    private IAppUserService appUserService;

    @RequestMapping(value = "/reg", name = "注册", method = RequestMethod.POST)
    public ResponseDTO register(@RequestBody RegisterDTO vo) {
        if (StringUtils.isEmpty(vo.getMobile())) {
            return ResponseDTO.error("手机号码不能为空");
        }
        if (StringUtils.isEmpty(vo.getSmsCode())) {
            return ResponseDTO.error("短信验证码不能为空");
        }
        /*if (StringUtils.isEmpty(vo.getPassword())) {
            return ResponseDTO.error("密码不能为空");
        }*/
        try {
            String authToken = appUserService.register(vo);
            return ResponseDTO.success(authToken);
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/login/pwd", name = "账号密码登录", method = RequestMethod.POST)
    public ResponseDTO pwdLogin(@RequestBody LoginPwdDTO dto) {
        if (StringUtils.isEmpty(dto.getMobile())) {
            return ResponseDTO.error("手机号码不能为空");
        }
        if (StringUtils.isEmpty(dto.getPassword())) {
            return ResponseDTO.error("密码不能为空");
        }
        try {
            String authToken = appUserService.pwdLogin(dto);
            return ResponseDTO.success(authToken);
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @ApiOperation(value = "手机检查", httpMethod = "POST")
    @RequestMapping(value = "/mobile/check/{mobile}", method = RequestMethod.POST)
    public ResponseDTO<String> mobileCheck(@PathVariable(value = "mobile") String mobile) {
        try {
            appUserService.mobileCheck(mobile);
            return ResponseDTO.success("手机验证通过");
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    @ApiOperation(value = "发送登录短信验证码", httpMethod = "POST")
    @RequestMapping(value = "/smsCode/login/{mobile}", method = RequestMethod.POST)
    public ResponseDTO<String> sendLoginSmsCode(@PathVariable(value = "mobile") String mobile) {
        appUserService.sendSmsVerifyCode(mobile,SmsTemplatesEnum.LOGIN_CONFIRM);
        return ResponseDTO.success("短信验证码已发送");
    }

    @ApiOperation(value = "发送注册短信验证码", httpMethod = "POST")
    @RequestMapping(value = "/smsCode/reg/{mobile}", method = RequestMethod.POST)
    public ResponseDTO<String> sendRegSmsCode(@PathVariable(value = "mobile") String mobile) {
        appUserService.sendSmsVerifyCode(mobile,SmsTemplatesEnum.USER_REGISTER);
        return ResponseDTO.success("短信验证码已发送");
    }

    @ApiOperation(value = "发送密码修改短信验证码", httpMethod = "POST")
    @RequestMapping(value = "/smsCode/reset-pass/{mobile}", method = RequestMethod.POST)
    public ResponseDTO<String> sendResetPasswordSmsCode(@PathVariable(value = "mobile") String mobile) {
        appUserService.sendSmsVerifyCode(mobile,SmsTemplatesEnum.RESET_PASSWORD);
        return ResponseDTO.success("短信验证码已发送");
    }

    @ApiOperation(value = "校验短信验证码", httpMethod = "POST")
    @RequestMapping(value = "/smsCode/login/check", method = RequestMethod.POST)
    public ResponseDTO checkSmsCode(@RequestBody MobileCheckDTO dto) {
        if (StringUtils.isEmpty(dto.getMobile())) {
            return ResponseDTO.error("手机号码不能为空");
        }
        if (StringUtils.isEmpty(dto.getSmsCode())) {
            return ResponseDTO.error("验证码不能为空");
        }
        boolean flag = appUserService.checkMobileCode(dto,SmsTemplatesEnum.LOGIN_CONFIRM);
        if (flag) {
            return ResponseDTO.success("校验通过");
        } else {
            return ResponseDTO.error("验证码错误");
        }
    }

    @ApiOperation(value = "校验注册验证码", httpMethod = "POST")
    @RequestMapping(value = "/smsCode/reg/check", method = RequestMethod.POST)
    public ResponseDTO checkRegSmsCode(@RequestBody MobileCheckDTO dto) {
        if (StringUtils.isEmpty(dto.getMobile())) {
            return ResponseDTO.error("手机号码不能为空");
        }
        if (StringUtils.isEmpty(dto.getSmsCode())) {
            return ResponseDTO.error("验证码不能为空");
        }
        boolean flag = appUserService.checkMobileCode(dto,SmsTemplatesEnum.USER_REGISTER);
        if (flag) {
            return ResponseDTO.success("校验通过");
        } else {
            return ResponseDTO.error("验证码错误");
        }
    }

    @ApiOperation(value = "校验密码修改短信验证码", httpMethod = "POST")
    @RequestMapping(value = "/smsCode/reset-pass/check", method = RequestMethod.POST)
    public ResponseDTO checkResetPasswordSmsCode(@RequestBody MobileCheckDTO dto) {
        if (StringUtils.isEmpty(dto.getMobile())) {
            return ResponseDTO.error("手机号码不能为空");
        }
        if (StringUtils.isEmpty(dto.getSmsCode())) {
            return ResponseDTO.error("验证码不能为空");
        }
        boolean flag = appUserService.checkMobileCode(dto,SmsTemplatesEnum.LOGIN_CONFIRM);
        if (flag) {
            return ResponseDTO.success("校验通过");
        } else {
            return ResponseDTO.error("验证码错误");
        }
    }
    @ApiOperation(value = "短信验证码登录验证", httpMethod = "POST")
    @RequestMapping(value = "/login/smsCode", method = RequestMethod.POST)
    public ResponseDTO smsCodeLogin(@RequestBody MobileCheckDTO dto) {
        if (StringUtils.isEmpty(dto.getMobile())) {
            return ResponseDTO.error("手机号码不能为空");
        }
        if (StringUtils.isEmpty(dto.getSmsCode())) {
            return ResponseDTO.error("验证码不能为空");
        }
        try {
            String authToken = appUserService.smsCodeLogin(dto);
            return ResponseDTO.success(authToken);
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @ApiOperation(value = "重置密码", httpMethod = "POST")
    @RequestMapping(value = "/reset-pass", method = RequestMethod.POST)
    public ResponseDTO resetAppPassword(@RequestBody AppUserResetPassVo vo) {
        if (StringUtils.isEmpty(vo.getMobile())) {
            return ResponseDTO.error("手机号码不能为空");
        }
        if (StringUtils.isEmpty(vo.getPassword())) {
            return ResponseDTO.error("密码不能为空");
        }
        try {
            appUserService.resetAppPassword(vo);
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @ApiOperation(value = "退出登录", httpMethod = "POST")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public ResponseDTO logout(@RequestHeader String authorization) {
        UserSubjectUtil.removeAppUser(authorization);
        return ResponseDTO.success();
    }

    @ApiOperation(value = "验证用户token是否过期", httpMethod = "GET")
    @RequestMapping(value = "/checkAuth", method = RequestMethod.GET)
    public ResponseDTO checkAuth( String  authToken){
        AppUser appUser = UserSubjectUtil.getAppUser(authToken);
        if(null==appUser){
            return  ResponseDTO.error("未登录");
        }
        return ResponseDTO.success(appUser);
    }

    @RequestMapping(value = "/info", name = "用户信息", method = RequestMethod.GET)
    public ResponseDTO userInfo(@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        if (null == appUser) {
            return ResponseDTO.error("登录已过期");
        }
        AppUserVo vo = appUserService.findByUserId(appUser.getUserId());
        return ResponseDTO.success(vo);
    }

    @ApiOperation(value = "修改签名", httpMethod = "POST")
    @RequestMapping(value = "/signature", method = RequestMethod.POST)
    public ResponseDTO signature(@RequestBody AppUserEditDTO vo,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        try {
            appUserService.editSignature(appUser.getUserId(),vo.getSignature());
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @ApiOperation(value = "修改头像", httpMethod = "POST")
    @RequestMapping(value = "/headImg", method = RequestMethod.POST)
    public ResponseDTO headImg(@RequestBody AppUserEditDTO vo,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        try {
            appUserService.editHeadImage(appUser.getUserId(),vo.getHeadImg());
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @ApiOperation(value = "修改昵称", httpMethod = "POST")
    @RequestMapping(value = "/nickName", method = RequestMethod.POST)
    public ResponseDTO nickName(@RequestBody AppUserEditDTO vo,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        try {
            appUserService.editNickName(appUser.getUserId(),vo.getNickName());
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @ApiOperation(value = "修改性别", httpMethod = "POST")
    @RequestMapping(value = "/sex", method = RequestMethod.POST)
    public ResponseDTO sex(@RequestBody AppUserEditDTO vo,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        try {
            appUserService.editSex(appUser.getUserId(),vo.getSex());
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @ApiOperation(value = "修改生日", httpMethod = "POST")
    @RequestMapping(value = "/birthday", method = RequestMethod.POST)
    public ResponseDTO birthday(@RequestBody AppUserEditDTO vo,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        try {
            appUserService.editBirthDay(appUser.getUserId(),vo.getBirthday());
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @ApiOperation(value = "修改所在地", httpMethod = "POST")
    @RequestMapping(value = "/address", method = RequestMethod.POST)
    public ResponseDTO address(@RequestBody AppUserEditDTO vo,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        try {
            vo.setUserId(appUser.getUserId());
            appUserService.editAddress(vo);
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }
}
